<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="layui/css/layui.css"/>
    <style>
        .layui-inline{
            margin-top: 10px;
        }
        .layui-table-box {
            overflow-y: scroll;
            width: 100%;
            max-height: 400px;
        }
    </style>
    <title>授权设备号EXCEL生成</title>

</head>

<body>
<div class="layui-btn-container txt" style="margin-top: 10px;margin-left: 10px;display: none">
    <input  type="file" class="layui-btn" id="fileInput" accept=".xls,.xlsx,.txt">
</div>

<div class="layui-form layui-col-md12 x-so" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">开始设备id:</label>
        <div class="layui-input-inline ">
            <div>
                <input type="text" class="layui-input" id="deviceId" autocomplete=”off” oninput="onInputChange(event)">
            </div>
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label" >数量:</label>
        <div class="layui-input-inline ">
            <div>
                <input type="text" class="layui-input" id="number" autocomplete=”off”>
            </div>
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">渠道号:</label>
        <div class="layui-input-inline" style="min-width: 250px">
            <div id="channel"></div>
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">是否只授权:</label>
        <div class="layui-input-inline ">
            <select id="val" name="val">
                <option value="0">否</option>
                <option value="1">是</option>
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">导出列:</label>
        <div class="layui-input-inline" style="min-width: 250px">
            <div id="column"></div>
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline ">
            <button class="layui-btn  generate">生成</button>
        </div>
    </div>
</div>
<table id="recharge" lay-filter="recharge"></table>
<script src="jquery.min-1.12.4.js"></script>
<script src="FileSaver.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.5/xlsx.full.min.js"></script>
<script src="layui/layui.all.js"></script>
<script src="xm-select.js"></script>
<script>
    var listdata = []
    var newdata = [
        {
            deviceId: "安卓Id",
            channel: "渠道",
            settleType: "只授权(默认为0，博乐的为1)"
        }
    ]
    var ejdata = [
        {
            deviceId: "设备id",
            channel: "渠道号",
        }
    ]
    document.getElementById('fileInput').addEventListener('change', function(e) {
        listdata = []
        ejdata = [
            {
                deviceId: "设备id",
                channel: "渠道号",
            }
        ]
        var file = e.target.files[0];
        var fileName = file.name.substring(file.name.lastIndexOf(".") + 1).toLowerCase();
        console.log(fileName)
        var reader = new FileReader();


            if(fileName=="txt"){
                reader.onload = function (e) {
                    var content = e.target.result;
                    const lines = content.split('\n');
                    const processedLines = lines.map(line => {
                        // 替换、切割等操作
                        if(line.split('$').length<2){
                            if(line != ""){
                                listdata.push(
                                    {
                                        deviceId: line,
                                        channel: xmselects.getValue("value")[0],
                                    }
                                );
                                ejdata.push({
                                    deviceId: line,
                                    channel: xmselects.getValue("value")[0],
                                })
                            }

                        }else{
                            if(line != ""){
                                listdata.push(
                                    {
                                        deviceId: line.replaceAll(':', '').replace("\r", '').split('$')[4],
                                        channel: xmselects.getValue("value")[0],
                                    }
                                );
                                ejdata.push({
                                    deviceId: line.replaceAll(':', '').replace("\r", '').split('$')[4],
                                    channel: xmselects.getValue("value")[0],
                                })
                            }
                        }
                    });
                    console.log(listdata)
                    layui.use(['table', 'excel'], function () {
                        var table = layui.table;
                        var excel = layui.excel;
                        //第一个实例
                        var rendertable = function () {
                            table.render({
                                elem: '#recharge'
                                // , toolbar: '#toolbarDemo'
                                , page: false
                                , limit: listdata.length
                                , cols: [
                                    [
                                        {field: 'deviceId', title: '设备id', width: 250}
                                        , {field: 'channel', title: '渠道号'}

                                    ]
                                ],
                                data: listdata
                            });

                        }
                        rendertable()
                        var colConf = excel.makeColConfig({
                            'A': 150,
                            'B': 150,
                            'C': 100,
                        }, 100);
                        var rowConfig = excel.makeRowConfig({
                            1: 25,
                        }, 40);
                        excel.exportExcel({sheet1: excel.filterExportData(ejdata, xmselects1.getValue("value"))}, '数据.xlsx', 'xlsx',{
                            extend: {
                                '!cols': colConf,
                                '!rows': rowConfig
                            }
                        });
                    })

                }
                reader.readAsText(file);

            }else {
                newdata = [
                    {
                        deviceId: "安卓Id",
                        channel: "渠道",
                        settleType: "只授权(默认为0，博乐的为1)"
                    }
                ]
                reader.onload = function (e) {
                    var data = e.target.result;
                    var workbook = XLSX.read(data, {type: 'binary'});

                    // 读取第一个工作表
                    var sheetName = workbook.SheetNames[0];
                    var worksheet = workbook.Sheets[sheetName];
                    // 将工作表转换为JSON
                    var json = XLSX.utils.sheet_to_json(worksheet);
                    var listkey
                    //第一行为设备号，不带（安卓Id或者渠道号
                    var addition=false
                    json.forEach((item) => {
                        for (var key in item) {
                            listkey=key
                        }
                        if(!(/.*[\u4e00-\u9fa5]+.*$/.test(key))&& addition == false){
                            listdata.push(
                                {
                                    deviceId: key,
                                    channel: xmselects.getValue("value")[0],
                                    settleType: $("#val").val(),
                                }
                            );
                            addition=true
                        }
                        listdata.push(
                            {
                                deviceId: item[listkey],
                                channel: xmselects.getValue("value")[0],
                                settleType: $("#val").val(),
                            }
                        );
                        newdata.push({
                            deviceId: item[listkey],
                            channel: xmselects.getValue("value")[0],
                            settleType: $("#val").val(),
                        })
                    });
                    layui.use(['table', 'excel'], function () {
                        var table = layui.table;
                        var excel = layui.excel;
                        //第一个实例
                        var rendertable = function () {
                            table.render({
                                elem: '#recharge'
                                // , toolbar: '#toolbarDemo'
                                , page: false
                                , limit: listdata.length
                                , cols: [
                                    [
                                        {field: 'deviceId', title: '安卓Id', width: 250}
                                        , {field: 'channel', title: '渠道'}
                                        , {
                                        field: 'settleType', title: '只授权(默认为0，博乐的为1)',
                                    }

                                    ]
                                ],
                                data: listdata
                            });

                        }
                        rendertable()
                        var colConf = excel.makeColConfig({
                            'A': 150,
                            'B': 150,
                            'C': 100,
                        }, 100);
                        var rowConfig = excel.makeRowConfig({
                            1: 25,
                        }, 40);
                        excel.exportExcel({sheet1: excel.filterExportData(newdata, xmselects1.getValue("value"))}, '数据.xlsx', 'xlsx',{
                            extend: {
                                '!cols': colConf,
                                '!rows': rowConfig
                            }
                        });
                    })

                }
                    reader.readAsBinaryString(file);
                }
    })

    layui.config({
        base: 'layui/module/layui-excel/layui_exts/' // 设置layui-ext的目录路径
    }).extend({
        excel: 'excel' // 如果excel.js是在layui-ext目录下的modules文件夹内，则不需要设置路径
    });
    $(function () {
        getstatus()
        getcolumn()

    })
    //输入监听
    function onInputChange(event){
        console.log(event.target.value);
        if(event.target.value != ''){
            $(".txt").hide()
        }else {
            console.log(xmselects.getValue("value")[0])
            if(xmselects.getValue("value")[0]!=undefined){
                $(".txt").show()
            }else{
                $(".txt").hide()
            }
        }
    }
    $(".generate").click(function () {
        var lastLetterIndex = findlastLetterIndex($("#deviceId").val())
        var result=findAndTrimString($("#deviceId").val(),lastLetterIndex)
        console.log(result)
        if ($("#deviceId").val() == "") {
            layer.msg("开始设备Id不能为空")
            return
        }
        if ($("#number").val() < 1) {
            layer.msg("生成数量不能小于1")
            return
        }
        console.log(xmselects.getValue("value"))
        if (xmselects.getValue("value") == "") {
            layer.msg("渠道号不能为空")
            return
        }
        listdata = []
        newdata = [
            {
                deviceId: "安卓Id",
                channel: "渠道",
                settleType: "只授权(默认为0，博乐的为1)"
            }
        ]
        for (var i = 0; i < $("#number").val(); i++) {
            var newtrimmedAfter=(parseInt(result.trimmedAfter) + i)
            console.log(result.trimmedAfter.length)
            console.log((parseInt(result.trimmedAfter) + i).toString().length)
            if(result.trimmedAfter.length>(parseInt(result.trimmedAfter) + i).toString().length){
                for(var j=0;j<(result.trimmedAfter.length-(parseInt(result.trimmedAfter) + i).toString().length);j++){
                    newtrimmedAfter="0"+newtrimmedAfter
                }
            }
            listdata.push({
                deviceId: result.trimmedBefore + newtrimmedAfter,
                channel: xmselects.getValue("value")[0],
                settleType: $("#val").val(),
            })
            newdata.push({
                deviceId: result.trimmedBefore + newtrimmedAfter,
                channel: xmselects.getValue("value")[0],
                settleType: $("#val").val(),
            })
        }
        layui.use(['table', 'excel'], function () {
            var table = layui.table;
            var excel = layui.excel;
            //第一个实例
            var rendertable = function () {
                table.render({
                    elem: '#recharge'
                    // , toolbar: '#toolbarDemo'
                    , page: false
                    , limit: $("#number").val()
                    , cols: [
                        [

                            {field: 'deviceId', title: '安卓Id', width: 250}
                            , {field: 'channel', title: '渠道'}
                            , {
                            field: 'settleType', title: '只授权(默认为0，博乐的为1)',
                        }


                        ]
                    ],
                    data: listdata
                });

            }
            rendertable()
            var colConf = excel.makeColConfig({
                'A': 150,
                'B': 150,
                'C': 100,
            }, 100);
            var rowConfig = excel.makeRowConfig({
                1: 25,
            }, 40);

            excel.exportExcel({sheet1: excel.filterExportData(newdata, xmselects1.getValue("value"))}, '数据.xlsx', 'xlsx',{
                extend: {
                    '!cols': colConf,
                    '!rows': rowConfig
                }
            });
        })
    })
    function getstatus() {
        var seletedata = [
            {
                name: "O_30_ACOFA_BB",
                value: "O_30_ACOFA_BB"
            },
            {
                name: "O_50_ACOFA_BB_A50",
                value: "O_50_ACOFA_BB_A50"
            },
            {
                name: "O_40_ACOFA_BB",
                value: "O_40_ACOFA_BB",

            },
            {
                name: "O_40_ACOFA_BB_ZE_A30_HKT",
                value: "O_40_ACOFA_BB_ZE_A30_HKT",
            },
            {
                name: "O_40_ACOFA_BB_A20_WIFI_BLT_CLIENT",
                value: "O_40_ACOFA_BB_A20_WIFI_BLT_CLIENT"
            },
            {
                name: "O_40_ACOFA_BB_A20_HKT",
                value: "O_40_ACOFA_BB_A20_HKT",
            },
            {
                name: "O_40_ACOFA_BB_A20",
                value: "O_40_ACOFA_BB_A20",
            },
            {
                name: "O_40_ACOFA_BB_A10",
                value: "O_40_ACOFA_BB_A10",
            },
            {
                name: "A_24_WFCA_BB",
                value: "A_24_WFCA_BB",
            },
            {
                name: "O_30_ACOFA_BB_OFFLINE",
                value: "O_30_ACOFA_BB_OFFLINE",
            },
            {
                name: "O_40_ACOFA_BB_A20_WIFI_BLT_SERVER",
                value: "O_40_ACOFA_BB_A20_WIFI_BLT_SERVER",
            },
            {
                name: "O_40_ACOFA_BB_A20_5610",
                value: "O_40_ACOFA_BB_A20_5610",
            },
            {
                name: "O_40_ACOFA_BB_5611",
                value: "O_40_ACOFA_BB_5611",
            },
            {
                name: "O_40_ACOFA_BB_A20_HKT_5611",
                value: "O_40_ACOFA_BB_A20_HKT_5611",
            },
            {
                name: "O_40_ACOFA_BB_A20_HKT_5610",
                value: "O_40_ACOFA_BB_A20_HKT_5610",
            },
            {
                name: "K_20_ACOFA_BL",
                value: "K_20_ACOFA_BL",
            },
            {
                name: "O_30_ACOFA_BL",
                value: "O_30_ACOFA_BL",
            },
            {
                name: "K_35_ACOFA_BL",
                value: "K_35_ACOFA_BL",
            },
            {
                name: "K_35_WFCA_BL_UG",
                value: "K_35_WFCA_BL_UG",
            },
            {
                name: "A_30_ACOF_BL17K",
                value: "A_30_ACOF_BL17K",
            },
            {
                name: "M_40_ACOFA_BL",
                value: "M_40_ACOFA_BL",
            },
            {
                name: "L_30_ACOFA_BL",
                value: "L_30_ACOFA_BL",
            },
            {
                name: "N_24_ACOFA_LLM10",
                value: "N_24_ACOFA_LLM10",
            },
            {
                name: "L_30_ACOF_LLM9_GTPB",
                value: "L_30_ACOF_LLM9_GTPB",
            },
            {
                name: "N_30_ACOFA_LLZ9_PL",
                value: "N_30_ACOFA_LLZ9_PL",
            },
            {
                name: "N_24_ACOF_LLS9",
                value: "N_24_ACOF_LLS9",
            },
            {
                name: "N_24_ACOF_LLS9A",
                value: "N_24_ACOF_LLS9A",
            },
            {
                name: "L_30_ACOFA_LLZ9",
                value: "L_30_ACOFA_LLZ9",
            },
            {
                name: "N_30_ACOF_LLM9_GTPB",
                value: "N_30_ACOF_LLM9_GTPB",
            },
            {
                name: "N_30_ACOFA_LL",
                value: "N_30_ACOFA_LL",
            }, {
                name: "M_40_ACOF_WIFI_TSH_GTV600S",
                value: "M_40_ACOF_WIFI_TSH_GTV600S",
            }, {
                name: "H_30_ACOF_GTV10",
                value: "H_30_ACOF_GTV10",
            }, {
                name: "N_40_ACOF_TSHV600_GTTP",
                value: "N_40_ACOF_TSHV600_GTTP",
            }, {
                name: "O_50_ACOF_WIFI_TSH_V12",
                value: "O_50_ACOF_WIFI_TSH_V12",
            }, {
                name: "M_30_ACOF_TSH",
                value: "M_30_ACOF_TSH",
            }, {
                name: "H_40_ACOF_TWVI_YM",
                value: "H_40_ACOF_TWVI_YM",
            },{
                name: "H_40_ACOFA_TWVI",
                value: "H_40_ACOFA_TWVI",
            },{
                name: "H_40_ACOFA_TWCASA",
                value: "H_40_ACOFA_TWCASA",
            },{
                name: "H_40_ACOF_TWGJ",
                value: "H_40_ACOF_TWGJ",
            },{
                name: "H_40_4G_TWCS",
                value: "H_40_4G_TWCS",
            },{
                name: "H_40_ACOFA_TWVI_YM",
                value: "H_40_ACOFA_TWVI_YM",
            },{
                name: "O_30_ACOF_TWDD",
                value: "O_30_ACOF_TWDD",
            },{
                name: "O_30_ACOF_TWYCX",
                value: "O_30_ACOF_TWYCX",
            },{
                name: "H_40_ACOF_TW",
                value: "H_40_ACOF_TW",
            },{
                name: "H_40_ACOF_TWVI",
                value: "H_40_ACOF_TWVI",
            },{
                name: "H_30_ACOFA_WXSD",
                value: "H_30_ACOFA_WXSD",
            },{
                name: "N_24_ACOFA_WXSD",
                value: "N_24_ACOFA_WXSD",
            },{
                name: "K_24_WFCA_WXSD",
                value: "K_24_WFCA_WXSD",
            },{
                name: "K_24_WFCA_WXSD_HEADSET",
                value: "K_24_WFCA_WXSD_HEADSET",
            },{
                name: "K_24_WFCA_WXSD_NTL3000",
                value: "K_24_WFCA_WXSD_NTL3000",
            },{
                name: "H_30_ACOFA_YINSHUO",
                value: "H_30_ACOFA_YINSHUO",
            },{
                name: "HK_CURRENT",
                value: "HK_CURRENT",
            },
            {
                name: "EJ_TSH",
                value: "EJ_TSH",
            },
            {
                name: "BT_TSH",
                value: "BT_TSH",
            },{
                name: "RECORD",
                value: "RECORD",
            },{
                name: "ZDKJ",
                value: "ZDKJ",
            },{
                name: "LYXF_IMAGE_TRANS",
                value: "LYXF_IMAGE_TRANS",
            },{
                name: "LYXF_IMAGE_VIDEO",
                value: "LYXF_IMAGE_VIDEO",
            },{
                name: "LYXF_COMMON",
                value: "LYXF_COMMON",
            },{
                name: "WONDER_TALKIE",
                value: "WONDER_TALKIE",
            },{
                name: "WONDER_TALKIE_PHOTO",
                value: "WONDER_TALKIE_PHOTO",
            },{
                name: "WONDER_TALKIE_SI",
                value: "WONDER_TALKIE_SI",
            },{
                name: "WONDER_TALKIE_VIDEO",
                value: "WONDER_TALKIE_VIDEO",
            },{
                name: "WONDER_TALKIE_VIDEO_SI",
                value: "WONDER_TALKIE_VIDEO_SI",
            },{
                name: "TT_TALK",
                value: "TT_TALK",
            },
        ]
        xmselects = xmSelect.render({
            // 这里绑定css选择器
            el: '#channel',
            on: function(data){
                //当前选中数据
               if($("#deviceId").val() == ''&& data != null){
                   $(".txt").show()
               }
            },
            model: {
                label: {
                    type: 'block',
                    block: {
                        //最大显示数量, 3:不限制
                        showCount: 1,
                        //是否显示删除图标
                        showIcon: true,
                    }
                }
            },
            tips: "请选择渠道",
            // toolbar: {
            //     show: true,
            // },
            filterable: true,
            radio: true,//单选
            // 渲染的数据
            data: seletedata,
        })

    }

    function getcolumn() {
        var columndata = [
            {
                name: "安卓Id",
                value: "deviceId",
                selected: true
            },{
                name: "渠道",
                value: "channel",
                selected: true
            },{
                name: "是否只授权",
                value: "settleType",
                selected: true
            },
        ]
        xmselects1 = xmSelect.render({
            // 这里绑定css选择器
            el: '#column',
            model: {
                label: {
                    type: 'block',
                    block: {
                        //最大显示数量, 3:不限制
                        showCount: 3,
                        //是否显示删除图标
                        showIcon: true,
                    }
                }
            },
            tips: "请选择列",
            toolbar: {
                show: true,
            },
            filterable: true,
            // radio: true,//单选
            // 渲染的数据
            data: columndata,
        })

    }



    function findlastLetterIndex(str) {
        for (let i = str.length - 1; i >= 0; i--) {
            if (str[i].match(/[A-Za-z]/)) {
                return i;
            }
        }
        return -1; //
    }
    function findAndTrimString(str,index) {
        const trimmedBefore = str.slice(0, index + 1);
        // 后剪：从最后一个字母之后到结束
        const trimmedAfter = str.slice(index + 1);

        return {trimmedBefore: trimmedBefore, trimmedAfter: trimmedAfter}
    }
</script>
</body>

</html>

